﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Guoc.DAL.Interface;
using Guoc.DAL.Domain;
using Guoc.DAL.Common;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Transform;
using NHibernate.Criterion;
using System.Web;

namespace Guoc.DAL.Common
{
    public class BaseService<T> :IDataAccess<T> where T : BaseEntity,new()
    {
        /// <summary>
        /// 数据操作当前Session
        /// </summary>
        public ISession CurrentSession
        {
            get {

                return NhibernateHelper.GetCurrentSession();              
            }            
        }

       
        public bool Create(T t)
        {
            bool result = false;
            try
            {
                CurrentSession.Save(t);
                CurrentSession.Flush();
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public bool Update(T t)
        {
            bool result = false;
            try
            {
                CurrentSession.Update(t);
                CurrentSession.Flush();
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public bool Delete(T t)
        {
            bool result = false;
            try
            {   
                CurrentSession.Delete(t);
                CurrentSession.Flush();
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        

        /// <summary>
        /// 查询id的单个实体，无记录时，返回实体ID为空字符串
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T Get(object id)
        {
            T t = new T();
            try
            {
               
                t = CurrentSession.Get<T>(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return t;
        }
        /// <summary>
        /// 判断某列,或某实体属性值是否存在
        /// </summary>
        /// <param name="columnName">列名，属性名</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public bool IsExists(string columnName, string value)
        {
            bool result = false;
            try
            {

                ICriteria iquery = CurrentSession.CreateCriteria(typeof(T))
                    .Add(Expression.Eq(columnName, value));
                IList<T> list = iquery.List<T>();
                if (list.Count > 0)
                {
                    result = true;
                }
                CurrentSession.Flush();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 返回所有T的所有记录列表，数据量大表尽量少调用
        /// </summary>
        /// <returns></returns>
        public List<T> GetList()
        {
            List<T> query = null;
            try
            {   
                query = CurrentSession.Query<T>().ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return  query;
        }        

        /// <summary>
        /// 返回分页的列表集合
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="rows">每页显示记录数</param>
        /// <param name="count">总记录数</param>
        /// <returns></returns>
        public List<T> GetList(int page, int rows, out int count)
        {           
            List<T> query = null;
            int total = 0;
            try
            {
                query = CurrentSession.Query<T>()
                        .Skip((page - 1) * rows).Take(rows)
                        .ToList();
                total = CurrentSession.Query<T>().Count();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            count = total;
            return  query;
        }
        /// <summary>
        /// 执行HQL 语句
        /// </summary>
        /// <param name="hql">hql语句</param>
        /// <returns></returns>
        public bool ExecuteHql(string hql)
        {
            bool result = false;
            try
            {
                CurrentSession.CreateQuery(hql).ExecuteUpdate();               
                CurrentSession.Flush();
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

     
        /// <summary>
        /// 执行原生Sql语句。示例：query="Delete from sys_Role where id in ('qq','ee')"; 
        /// </summary>
        /// <param name="query">SQL语句</param>
        /// <returns></returns>
        //public bool ExecuteSql(string query)
        //{
        //    bool result = false;
        //    try
        //    {

        //        ISQLQuery iquery = CurrentSession.CreateSQLQuery(query);
        //        iquery.ExecuteUpdate();
        //        CurrentSession.Flush();
        //        result = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return result;
        //}

        /// <summary>
        /// Linq查询接口
        /// </summary>
        public IQueryable<T> LinqQuery
        {
            get { return CurrentSession.Query<T>(); }
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        public ICriteria ConditionQuery
        {
            get { return CurrentSession.CreateCriteria<T>(); }
        }
       
    }
}
